//
//  SwiftUIView.swift
//  
//
//  Created by li on 2022/2/18.
//

import SwiftUI
import AVFoundation
import PhotosUI
//import MobileCoreServices

public struct DogPHCameraConfiguration {

    public var allowEditing: Bool = true
//    var mediaType: String = UTType.image.identifier

    public init() {
        
    }
}

/// 相机视图
/// alert 如果相机没权限,根据权限配置默认展示视图
public struct DogPHCamera<Alert: View>: View {
    
    @Binding private var image: UIImage?
    private var alert: () -> Alert
    
    @State private var isAvalibleOfCamera: Bool = false
    private let configuration: DogPHCameraConfiguration
    public init(_ configuration: DogPHCameraConfiguration = DogPHCameraConfiguration(), image: Binding<UIImage?>, alert: @escaping () -> Alert) {
        
        self.configuration = configuration
        self._image = image
        self.alert = alert
        
        isAvalibleOfCamera = UIImagePickerController.isSourceTypeAvailable(.camera) && AVCaptureDevice.authorizationStatus(for: .video) == .authorized
    }
    
    public var body: some View {

        ZStack  {
            if isAvalibleOfCamera {

                DogPHCameraRepresentable(configuration, image: $image)
            }else {
                alert()
            }
        }
        .onAppear {
            isAvalibleOfCamera = UIImagePickerController.isSourceTypeAvailable(.camera) && AVCaptureDevice.authorizationStatus(for: .video) == .authorized
        }
        
    }
}


struct DogPHCameraRepresentable: UIViewControllerRepresentable {

    @ Binding fileprivate var image: UIImage?
    @ Environment(\.presentationMode) fileprivate var presentation
    private let configuration: DogPHCameraConfiguration
    
    public init(_ configuration: DogPHCameraConfiguration,  image: Binding<UIImage?>) {
        
        self.configuration = configuration
        self._image = image
    }
    
    public func makeUIViewController(context: Context) -> UIImagePickerController {
         
        let controller = UIImagePickerController()
        controller.sourceType = .camera
//        controller.mediaTypes = [UTType.movie.identifier, UTType.image.identifier]
        controller.mediaTypes = [UTType.image.identifier]
        controller.delegate = context.coordinator
        controller.allowsEditing = configuration.allowEditing
        return controller
    }
    
    public func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {
    
    }
    
    
    public func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    public class Coordinator: NSObject, UINavigationControllerDelegate,UIImagePickerControllerDelegate {
        var parent: DogPHCameraRepresentable
        
        init(_ parent: DogPHCameraRepresentable) {
            self.parent = parent
        }
        
        public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
            parent.presentation.wrappedValue.dismiss()

            guard let selectedImage = info[picker.allowsEditing ? .editedImage : .originalImage] as? UIImage else { return }
            
            parent.image = selectedImage
            
        }
    }
}

